/**
 移动页面Controller
 @class logs
 @author :laiwp
 @date:  :2016-07-01
 @version : 0.1.0
 @constructor : 0.1.0
 */
var config = require('../settings');
var tools = require('../tools.js');
var https = require('https');
var async = require('async');
var md5 = require('md5');
var moment = require('moment');
var superagent = require('superagent');
var dbDao = require('../models/dbBase.js');

module.exports = {
    mapping: {
        _sport_list: {
            'url': '/sport/list',
            'method': 'get',
            'description': '场馆列表',
            'auth': false
        },
        _sport_details: {
            'url': '/sport/details',
            'method': 'get',
            'description': '场馆详情',
            'auth': false
        },

        _sport_gymresource: {
            'url': '/sport/gymresource',
            'method': 'get',
            'description': '场地资源页面',
            'auth': false
        },

        _sport_getgymresource: {
            'url': '/sport/gym/resource',
            'method': 'get',
            'description': '获取场地资源',
            'auth': false
        },

        _prebook_findfreeresource: {
            'url': '/prebook/findFreeResource',
            'method': 'get',
            'description': '查询可用场地',
            'auth': false
        },
    },

    _prebook_findfreeresource : function (req,res){
        var fieldId = req.param('fieldId') || '';
        var startTime = req.param('startTime') || '';
        var hours = req.param('hours') || '';
        var date = req.param('date') || '';

        console.log("---params:", fieldId);
        superagent.get('http://123.57.72.140:3003/prebook/findFreeResource')
            .query('fieldId='+fieldId)
            .query('startTime='+startTime)
            .query('hours='+hours)
            .query('date='+date)
            .end(function(err, result){
              if(err){

                        console.log("error:", err);
                res.send({result:'error'});
              }else{
                if(result&&result.body){
                    console.log(result.body);
                    res.send(result.body);

                }else{
                    res.send({})
                }
              }

            });
    },

    //场馆首页
    _sport_list: function (req, res) {
        var sql = "select * from Gymnasium";
        dbDao.find( sql, function (err, result) {
            if(err){
                res.send({
                    locals: {
                        title: config.title,
                        data: []
                    }
                })
            }else{
                res.send({
                    locals: {
                        title: config.title,
                        data: result
                    }
                })
            }

        });

    },

    //场馆详情
    _sport_details: function (req, res) {
        console.log("进入资源详情页");
        var gymId = req.param('id');
        var sql = "select * from GymFields where gymId='" + gymId + "'";
        var data1;   //场馆信息
        var data2 = [];   //场馆的场地信息  和  日期价格
        var weeks = [];

        var day1 = moment().format('YYYYMMDD');
        var day2 = moment().add(1, 'days').format('YYYYMMDD');
        var day3 = moment().add(2, 'days').format('YYYYMMDD');
        var day4 = moment().add(3, 'days').format('YYYYMMDD');

        var w1 = moment().format('dddd');
        var w2 = moment().add(1, 'days').format('dddd');
        var w3 = moment().add(2, 'days').format('dddd');
        var w4 = moment().add(3, 'days').format('dddd');
        //var w5=moment().add(4, 'days').format('dddd');
        //var w6=moment().add(5, 'days').format('dddd');
        //var w7=moment().add(6, 'days').format('dddd');
        //var m1=day1.substring(4,6);
        //var d1=day1.substring(7,8);

        var md1 = tools.formatMD(day1.substring(4, 6)) + '月' + tools.formatMD(day1.substring(6, 8)) + '日';
        var md2 = tools.formatMD(day2.substring(4, 6)) + '月' + tools.formatMD(day2.substring(6, 8)) + '日';
        var md3 = tools.formatMD(day3.substring(4, 6)) + '月' + tools.formatMD(day3.substring(6, 8)) + '日';
        var md4 = tools.formatMD(day4.substring(4, 6)) + '月' + tools.formatMD(day4.substring(6, 8)) + '日';


        weeks = [{'d': day1, 'w': '今天', 'md': md1}, {'d': day2, 'w': tools.weeken(w2), 'md': md2},
            {'d': day3, 'w': tools.weeken(w3), 'md': md3}, {'d': day4, 'w': tools.weeken(w4), 'md': md4}];


        async.auto({
            findGymium: function (callback) {  //场馆信息
                dbDao.findById( ["Gymnasium", "id", gymId], function (err, result) {
                    data1 = result;
                    callback();
                });
            },
            findGymfield: function (callback) {    //场馆场地信息
                dbDao.find( sql, function (err, result2) {
                    if (result2.length > 0) {
                        var count = 0;
                        for (var i = 0; i < result2.length; i++) {
                                //var sql2 = "select * from GymResource where gymFieldId='" + gymFieldId + "' and gymId='" + gymId + "'";
                          data2.push(result2[i]);
                        }
                        callback();
                    } else {
                        callback();
                    }
                });
            },

        }, function (err, results) {
            res.send(
                {
                    title: config.title,
                    venueInfo: data1,
                    courts: data2,
                    weeks: weeks
                }
            )

        });
    },

    //场地资源页面
    _sport_gymresource: function (req, res) {
        var ddate = req.param('date') || moment().format('YYYYMMDD');
        console.log(req.param('date'));
        var gymId = req.param('gymid');
        var gymFiedid = req.param('fiedid');
        var gymname = req.param('gymname');

        var params = {'date': ddate, 'gymid': gymId, 'fiedid': gymFiedid, 'gymname': gymname};

        var day1 = moment().format('YYYYMMDD');
        var day2 = moment().add(1, 'days').format('YYYYMMDD');
        var day3 = moment().add(2, 'days').format('YYYYMMDD');
        var day4 = moment().add(3, 'days').format('YYYYMMDD');
        var day5 = moment().add(4, 'days').format('YYYYMMDD');
        var day6 = moment().add(5, 'days').format('YYYYMMDD');
        var day7 = moment().add(6, 'days').format('YYYYMMDD');

        var w1 = moment().format('dddd');
        var w2 = moment().add(1, 'days').format('dddd');
        var w3 = moment().add(2, 'days').format('dddd');
        var w4 = moment().add(3, 'days').format('dddd');
        var w5 = moment().add(4, 'days').format('dddd');
        var w6 = moment().add(5, 'days').format('dddd');
        var w7 = moment().add(6, 'days').format('dddd');

        var md1 = tools.formatMD(day1.substring(4, 6)) + '月' + tools.formatMD(day1.substring(6, 8)) + '日';
        var md2 = tools.formatMD(day2.substring(4, 6)) + '月' + tools.formatMD(day2.substring(6, 8)) + '日';
        var md3 = tools.formatMD(day3.substring(4, 6)) + '月' + tools.formatMD(day3.substring(6, 8)) + '日';
        var md4 = tools.formatMD(day4.substring(4, 6)) + '月' + tools.formatMD(day4.substring(6, 8)) + '日';
        var md5 = tools.formatMD(day5.substring(4, 6)) + '月' + tools.formatMD(day5.substring(6, 8)) + '日';
        var md6 = tools.formatMD(day6.substring(4, 6)) + '月' + tools.formatMD(day6.substring(6, 8)) + '日';
        var md7 = tools.formatMD(day7.substring(4, 6)) + '月' + tools.formatMD(day7.substring(6, 8)) + '日';
        weeks = [{'d': day1, 'w': '今天', 'md': md1}, {'d': day2, 'w': tools.weeken(w2), 'md': md2}, {
            'd': day3,
            'w': tools.weeken(w3),
            'md': md3
        }, {'d': day4, 'w': tools.weeken(w4), 'md': md4},
            {'d': day5, 'w': tools.weeken(w5), 'md': md5}, {'d': day6, 'w': tools.weeken(w6), 'md': md6}, {
                'd': day7,
                'w': tools.weeken(w7),
                'md': md7
            }
        ];

        res.send({
            title: config.title,
            params: params,
            weeks: weeks
        });


    },

    //获取场地资源
    _sport_getgymresource: function (req, res) {
        var ddate = req.param('date') || moment().format('YYYYMMDD');
        var gymId = req.param('gymid');
        var gymFiedid = req.param('fiedid');

        var sql = "select * from GymResource where date='" + ddate + "' and  gymId='" + gymId + "' and gymFieldId='" + gymFiedid + "' ";

        dbDao.find( sql, function (err, result2) {
            console.log(result2.length);
            res.send(result2);
        });


    },
}
